package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.PlateGridMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.PlateGrid;
import com.brillilab.service.core.experiments.IPlateGridService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 多孔板Grid
 * @author WuMenghao
 * @time   2019/04/18
 */
@SuppressWarnings("unchecked")
@Service
@Transactional
public class PlateGridServiceImpl implements IPlateGridService {

    @Resource
    private PlateGridMapper plateGridMapper;

    @Override
    public boolean insertBatch(List<PlateGrid> plateGrids) {
        return plateGridMapper.insertBatch(plateGrids) > 0;
    }

    @Override
    public boolean deleteByPlateId(Long plateId) {
        return plateGridMapper.deleteByPlateId(plateId) >0;
    }

    @Override
    public List<PlateGrid> selectList(Long plateId) {
        return plateGridMapper.selectList(
                new LambdaQueryWrapper<PlateGrid>()
                        .eq(PlateGrid::getPlateId,plateId)
                        .eq(PlateGrid::getIsDelete,BoolEnum.FALSE.getValue())
                        .orderByAsc(true,PlateGrid::getRowNo,PlateGrid::getColumnNo));
    }

    @Override
    public PlateGrid selectById(Long gridId) {
        return plateGridMapper.selectById(gridId);
    }

    @Override
    public boolean updateById(PlateGrid plateGrid) {
        plateGrid.setUpdateTime(new Date());
        return plateGridMapper.updateById(plateGrid) > 0;
    }

    @Override
    public List<PlateGrid> selectUsedGridList(Long plateId) {
        return plateGridMapper.selectList(
                new LambdaQueryWrapper<PlateGrid>()
                        .eq(PlateGrid::getPlateId,plateId)
                        .eq(PlateGrid::getIsUsed,BoolEnum.TRUE.getValue())
                        .eq(PlateGrid::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public List<PlateGrid> selectList(List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridMapper.selectList(
                    new LambdaQueryWrapper<PlateGrid>()
                            .in(PlateGrid::getId,gridIds)
                            .eq(PlateGrid::getIsDelete,BoolEnum.FALSE.getValue()));
        }
        return new ArrayList<>();
    }

    @Override
    public int updateBatch(List<PlateGrid> update) {
        if(update.size()>0){
            return plateGridMapper.updateBatch(update);
        }
        return 0;
    }

    @Override
    public boolean setIsUsed(List<Long> gridIds,Integer isUsed) {
        if(CollectionUtils.isNotEmpty(gridIds)){
            return plateGridMapper.setIsUsed(gridIds,isUsed) >=0;
        }
        return true;
    }
}
